<?php
namespace App\Rules;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest as FatherFormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;

/**
 * 返回验证出错的信息
 * 父类 FormRequest中的 failedValidation() 方法用来处理验证失败，我们重写父类方法，来实现自已的返回验证出错的信息
 * Class FormRequest
 * @package App\Rules
 */
class FormRequest extends FatherFormRequest
{
    /**
     * @param Validator $validator
     */
    protected function failedValidation(Validator $validator){
        $firstError = $validator->errors()->first();
        throw (new HttpResponseException(response()->json(['msg'=>$firstError, 'code'=>40000])));
    }
}
